package commom

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"gopkg.in/mgo.v2"
	"websocket/response"
)

var mongodb *mgo.Database

func InitMongodb() *mgo.Session {
	var c *gin.Context
	mongo, err := mgo.Dial(viper.GetString("mongodb.host"))
	if err != nil {
		response.FailWithServerError("mongodb连接失败", c)
	}
	return mongo
}

func GoDB() *mgo.Database {
	mongodb = InitMongodb().DB(viper.GetString("mongodb.db"))
	return mongodb
}

func GetMongoDB(c string) *mgo.Collection {
	return mongodb.C(c)
}
